package com.linkstec.bookshop.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.linkstec.bookshop.pojo.Comment;
import com.linkstec.bookshop.pojo.User;
import com.linkstec.bookshop.service.CommentService;
import com.linkstec.bookshop.util.PageBean;
import com.linkstec.bookshop.vo.CommentVo;

@Controller
@RequestMapping("comment")
public class CommentController {
	
	@Autowired
	private CommentService commentService;

	@RequestMapping(value = "findAllComment")
	public ModelAndView findAllComment(int page) {
		ModelAndView mv = new ModelAndView();
		PageBean<CommentVo> pageBean = new PageBean<CommentVo>();
			try {
				pageBean =commentService.findAllComment(page);
			} catch (Exception e) {
				e.printStackTrace();
			}
		mv.addObject("pageBean", pageBean);
		mv.setViewName("messageList");
		return mv;
	}
	@RequestMapping(value = "addComment")
	private String addComment(Comment comment,HttpServletRequest request) {
		try {
			User user = (User) request.getSession().getAttribute("loginUser");
			comment.setUserId(user.getUserId());
			commentService.addComment(comment);
		} catch (Exception e) {
			// TODO: handle exception
		}		
		return"redirect:/comment/findAllComment?page=1";
	}
}
